package com.example.demo.alarm.service.impl;

import com.example.demo.alarm.dao.AlarmSensor;
import com.example.demo.alarm.dao.AlarmSensorVo;
import com.example.demo.alarm.mapper.AlarmSensorMapper;
import com.example.demo.alarm.service.AlarmSensorService;
import com.example.demo.system.utility.PageInfo;
import com.example.demo.system.utility.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlarmSensorServiceImpl implements AlarmSensorService {

    @Autowired
    private AlarmSensorMapper alarmSensorMapper;

    @Override
    public Result getList(Integer pageNum, Integer pageSize, String name) {
        Integer start = PageInfo.start(pageNum, pageSize);
        //获取设备集合
        List<AlarmSensorVo> list = alarmSensorMapper.getDeviceList(start, pageSize, name);
        //获取设备总数
        Integer listCount = alarmSensorMapper.getDeviceListCount(name);
        //根据设备id获取设备下的传感器集合
        for (AlarmSensorVo alarmSensorVo : list) {
            alarmSensorVo.setAlarmSensorList(alarmSensorMapper.getList(alarmSensorVo.getDeviceId()));
        }
        PageInfo info = new PageInfo(pageNum, pageSize, listCount, list);
        return Result.success(info);
    }

    @Override
    public Result get(Integer deviceId) {
        //获取设备信息
        AlarmSensorVo device = alarmSensorMapper.getDevice(deviceId);
        //获取设备下的传感器集合
        device.setAlarmSensorList(alarmSensorMapper.getList(deviceId));
        return Result.success(device);
    }

    @Override
    public Result add(AlarmSensorVo alarmSensorVo) {
        //只处理传感器的数据
        List<AlarmSensor> alarmSensorList = alarmSensorVo.getAlarmSensorList();
        alarmSensorMapper.add(alarmSensorList);
        return Result.success("新增成功");
    }

    @Override
    public Result update(AlarmSensorVo alarmSensorVo) {
        List<AlarmSensor> alarmSensorList = alarmSensorVo.getAlarmSensorList();
        //要修改哪个传感器的数据就传递哪个传感器的数据
        //新增后无法通过修改增加或删除传感器的个数
        for (AlarmSensor alarmSensor : alarmSensorList) {
            alarmSensorMapper.update(alarmSensor);
        }
        return Result.success("修改成功");
    }

    @Override
    public Result delete(Integer sensorId) {
        alarmSensorMapper.delete(sensorId);
        return Result.success("删除成功");
    }

    @Override
    public Result deleteSensor(Integer deviceId) {
        alarmSensorMapper.deleteSensor(deviceId);
        return Result.success("删除成功");
    }


}
